package com.shop2cn.iapi.agentproducts.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.EditBatchProductRebateBean;
import com.shop2cn.iapi.agentproducts.service.EditBatchProductRebateCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.agentrebate.Ymt_AgentRebate;
import com.ymttest.database.model.agentrebate.Ymt_AgentRebateExample;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;

/**
 * 批量商品分佣设置Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_EditBatchProductRebate {
	private static EditBatchProductRebateBean editbatchproductrebateBean;
	private static EditBatchProductRebateCall editbatchproductrebateCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量商品分佣设置Test");
	}

	@Before
	public void caseUp() {
		editbatchproductrebateBean = new EditBatchProductRebateBean();
		editbatchproductrebateCall = new EditBatchProductRebateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(editbatchproductrebateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyMongoCurMq(String pid) throws ClientProtocolException, IOException, InterruptedException {
//		验证发送总线消息 commonService.syncMongoAction(req.getProductId(), req.getUserId(), UpdateAgentProductRebate);
		Thread.sleep(5000);
		MessageListBean MessageListBean2 = MqQueryCallService.initEditProductRebate(pid);

		List<Map> joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
		Logger.debug(joa2);
		int redo = 0;
		while (joa2.size() < 2 && redo < 3) {
			Thread.sleep(5000);
			joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
			redo++;
		}
		String actionType = "UpdateAgentProductRebate";
		String appId = "agentproducts.iapi.ymatou.com";
		Logger.verifyEquals(actionType, joa2.get(0).get("ActionType"), "验证ActionType");
		Logger.verifyEquals(pid, joa2.get(0).get("ProductId"), "验证ProductId");
		Logger.verifyEquals(appId, joa2.get(0).get("AppId"), "验证AppId");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_EditBatchProductRebate_001() {
		Logger.start(true, "正确数据，成功");
		try {
			int userId = 500945639;
			int AgentUserId = 500945640;
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			AddAgentProductBean addAgentProductBean2 = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId2 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean2);
			editbatchproductrebateBean = AddAgentProductCallService
					.initEditBatchProductRebateBean(Arrays.asList(productId, productId2), AgentUserId, userId);
			editbatchproductrebateCall.setData(editbatchproductrebateBean);
			editbatchproductrebateCall.callService();
			Logger.verifyEquals("200", editbatchproductrebateCall.getString("code"), "验证code");
		
			editbatchproductrebateBean.getProductIds().forEach(p -> {
				//查出设置商品的规格
				List<String> catalogs = new ArrayList<String>();
				try {
					catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(p).stream()
							.map(ele -> ele.getsCatalogId()).collect(Collectors.toList());
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				catalogs.forEach(x -> {
					Ymt_AgentRebateExample example = new Ymt_AgentRebateExample();
					editbatchproductrebateBean.getProductRebateList().forEach(r->{
						example.createCriteria().andCatalogIdEqualTo(x).andAgentTypeIdEqualTo(r.getAgentTypeId());
						//同一个规格同一个AgentTypeId应该能查出唯一值
						List<Ymt_AgentRebate> agnetrebates = SqlMapperFactory.getIntergratedProductMapper()
								.selectYmtAgentRebateExample(example);
						System.out.println(FormatUtil.beanToGSONString(agnetrebates));
						Logger.verifyEquals(r.getAgentTypeId(), agnetrebates.get(0).getAgentTypeId(), "验证getAgentTypeId");
						Logger.verifyEquals(x, agnetrebates.get(0).getCatalogId(), "验证getCatalogId");
						Logger.verifyEquals(r.getRebateValue().setScale(2),
								agnetrebates.get(0).getRebateValue().setScale(2), "验证getRebateValue");
						Logger.verifyEquals(editbatchproductrebateBean.getRebateType(), agnetrebates.get(0).getRebateType(),
								"验证getRebateType");
					});

				});

			});
			//验证mongo
			editbatchproductrebateBean.getProductIds().forEach(pid -> {
				try {
					verifyMongoCurMq(pid);
				} catch (IOException | InterruptedException e) {
					e.printStackTrace();
				}
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}